// pages/myCenter_opinion/mycenter_opinion.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },
    //设置当刷新后，富文本编辑器中的内容呈现上次缓存下来的内容
    cachetimer: null,
    cacheval: null,
    cacheContent: function (d) {
        this.cacheval = d.detail
        if (this.cachetimer) {
            return
        }
        // cachetimer 保存的是每一次的setTimeout的id值
        this.cachetimer = setTimeout(() => {
            // console.log(this.cacheval.html)//带标签
            // console.log(this.cacheval.text)//不带标签

            this.cachetimer = null;
            // wx.setStorageSync('cache-content', this.cacheval.html)
            wx.setStorageSync('cache-content', this.cacheval.text)
        }, 1000)

    },
    checkAndSetContent: function () {
        let content = wx.getStorageSync('cache-content')
        // 初始化编辑器内容
        content && this.editor.setContents({
            html: content,
            sucess: () => {
                console.log('ok')
            },
            fail: err => {
                console.log(err)
            }
        })
    },
    editorPost: function (f) {
        this.editor.getContents({
            success: res => {
                // console.log(res);
                // console.log(res.text);
            
                const token = wx.getStorageSync('token');
                wx.request({
                    url: `${app.globalData.url}/suggestion`,
                    method: 'POST',
                    dataType: 'json',
                    header: {
                        'content-type': 'text/json',
                        'Authorization': token
                    },
                    data: {
                        suggestionContent: res.text
                    },
                    success: res => {
                        // console.log(res);
                        if (res.data=='Token Is Null!') {
                            wx.showModal({
                                title: '未登录',
                                content: '您还没有登录哦，快去登录吧！',
                                confirmText: '去登录',
                                cancelText: '暂不登录',
                                success: res => {
                                    if (res.confirm) {
                                        wx.switchTab({
                                            url: '/pages/myCenter/myCenter'
                                        })
                                    }
                                }
                            })
                        }else{
                            // 提示成功
                            wx.showModal({
                                content:'提交成功！',
                                showCancel:false
                            })
                            this.editor.clear()
                            wx.removeStorageSync('cache-content');
                        }
                        
                    },
                    fail: err => {
                        console.log(err);
                    }
                })
            }
        })


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 提交建议
        let query = wx.createSelectorQuery()
        query.select('#editor').context(res => {

            //保存实例
            this.editor = res.context;

            this.checkAndSetContent()
        }).exec()


    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})